﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Edgar.Entities.MasterData;
using Edgar.Logic;
using Microsoft.Win32;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Markup;
using Edgar.Frontend.Wpf.CharacterSheet;
using Edgar.Frontend.Wpf.ViewModels;
using Edgar.Frontend.Wpf.Events;

namespace Edgar.Frontend.Wpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Rulebook rulebook;
        CharacterPersistenceController _characterPersistence;
        AddSkillDialog _addSkillDialog;

        public MainWindow()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;

            RulebookLoader loader = new RulebookLoader();
            try
            {
                rulebook = loader.Load("rulebook.xml");
            }
            catch (Exception e)
            {
                MessageBox.Show("Unbekannter Fehler beim Laden des Rulebook!\n" + e.Message);
                Application.Current.Shutdown();
            }

            Loaded += new RoutedEventHandler(MainWindow_Loaded);

            _characterPersistence = new CharacterPersistenceController();

            SkillsControl.AddButtonClick += SkillsControl_AddButtonClick;
            SkillsControl.RemoveButtonClick += SkillRemovedHandler;
        }

        void SkillsControl_AddButtonClick(object sender, EventArgs e)
        {
            _addSkillDialog = new AddSkillDialog(rulebook);
            _addSkillDialog.SkillAdded += SkillAddedHandler;
            _addSkillDialog.Owner = this;
            _addSkillDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            _addSkillDialog.ShowDialog();
        }

        void SkillAddedHandler(object sender, SkillEventArgs e)
        {
            ((CharacterViewModel)DataContext).AddSkill(e.Name, e.Category);
        }

        void SkillRemovedHandler(object sender, SkillEventArgs e)
        {
            ((CharacterViewModel)DataContext).RemoveSkill(e.Name, e.Category);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = new CharacterViewModel(rulebook);
        }

        private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void NewMenuItem_Click(object sender, RoutedEventArgs e)
        {
            DataContext = new CharacterViewModel(rulebook);
        }

        private void SaveMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = DataContext as CharacterViewModel;
            if (viewModel != null)
            {
                var character = viewModel.GetCharacter();

                var dialog = ConfigureCharacterFileDialog(new SaveFileDialog());
                if (character.Name != null)
                    dialog.FileName = character.Name.ToLower();
                if (dialog.ShowDialog() == true)
                {
                    _characterPersistence.Save(character, dialog.FileName);
                }
            }
        }

        private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var dialog = ConfigureCharacterFileDialog(new OpenFileDialog());
            if (dialog.ShowDialog() == true)
            {
                DataContext = new CharacterViewModel(rulebook, _characterPersistence.Load(dialog.FileName));
            }
        }

        private FileDialog ConfigureCharacterFileDialog(FileDialog dialog)
        {
            dialog.DefaultExt = ".edc";
            dialog.Filter = "EDGAR Characters (.edc)|*.edc";
            dialog.InitialDirectory = _characterPersistence.GetCharactersDirectory();
            return dialog;
        }

        private void PrintMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                var paginator = new CharacterSheetPaginator(
                    DataContext as CharacterViewModel,
                  new Size(printDialog.PrintableAreaWidth,
                    printDialog.PrintableAreaHeight));
                printDialog.PrintDocument(paginator, "My Random Data Table");
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (_addSkillDialog != null) _addSkillDialog.Close();
        }
    }
}

